(function($) {
	var methods = {
		parse_url : function(str, component) {
			var key = ['source', 'scheme', 'authority', 'userInfo', 'user', 'pass', 'host', 'port', 'relative', 'path', 'directory', 'file', 'query', 'fragment'],
				ini = (this.php_js && this.php_js.ini) || {},
				mode = (ini['phpjs.parse_url.mode'] && ini['phpjs.parse_url.mode'].local_value) || 'php',
				parser = {
					php: /^(?:([^:\/?#]+):)?(?:\/\/()(?:(?:()(?:([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?()(?:(()(?:(?:[^?#\/]*\/)*)()(?:[^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
					strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
					loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/\/?)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/ // Added one optional slash to post-scheme to catch file:/// (should restrict this)
				};
			var m = parser[mode].exec(str),
				uri = {},
				i = 14;
			while (i--) { if (m[i]) { uri[key[i]] = m[i]; } }
			if (component) { return uri[component.replace('PHP_URL_', '').toLowerCase()]; }
			if (mode !== 'php') {
				var name = (ini['phpjs.parse_url.queryKey'] && ini['phpjs.parse_url.queryKey'].local_value) || 'queryKey';
				parser = /(?:^|&)([^&=]*)=?([^&]*)/g;
				uri[name] = {};
				uri[key[12]].replace(parser, function ($0, $1, $2) { if ($1) {uri[name][$1] = $2;} });
			}
			delete uri.source;
			return uri;
		},

		getcookie : function(name) {
			var matches = document.cookie.match(new RegExp("(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)"))
			return matches ? decodeURIComponent(matches[1]) : undefined;
		},

		setcookie : function(name, value, expires, path, domain, secure) {
			expires instanceof Date ? expires = expires.toGMTString() : typeof(expires) == 'number' && (expires = (new Date(+(new Date) + expires * 1e3)).toGMTString());
			var r = [name + "=" + escape(value)], s, i;
			for(i in s = {expires: expires, path: path, domain: domain}){ s[i] && r.push(i + "=" + s[i]); }
			return secure && r.push("secure"), document.cookie = r.join(";"), true;
		}
	}

	$.fn.SubscribeWin = function() {
		var me = $(this);
		if ( document.referrer ) {
			var ref = document.referrer;
			var parts = methods.parse_url(ref);

      //alert('host = ' + parts.host + 'path = ' + parts.path);
 
      var sw = methods.getcookie('subscribewin');
      if ( sw !== undefined ) return;

  		if (
				(parts.host.match(/google/i) && ( parts.path.match(/search/i) || parts.path.match(/url/i))) ||
				(parts.host.match(/yandex/i) && parts.path.match(/yandsearch/i)) ||
				(parts.host.match(/mail/i) && parts.path.match(/search/i)) ||
				(parts.host.match(/rambler/i) && parts.path.match(/search/i))
			) {
				var d = new Date;
				d.setDate(d.getDate() + 7);
        document.cookie = 'subscribewin=1; expires=' + d.toUTCString();

        me.removeClass('sw-hide');
				me.addClass('sw-show');
				me.find('.sw-closed').click(function(){
					me.remove();
					location.reload();
				});
			}
		}
	}
})( jQuery );